package jie.service;

import jie.mapper.InfoMapper;
import jie.pojo.Account;
import jie.pojo.Record;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class InfoServiceImpl implements InfoService {

    @Resource
    private InfoMapper infoMapper;


    @Override
    public Account login(String cardno, String password) {
        Account login = infoMapper.login(cardno);
        if (login!=null && login.getPassword().equals(password)){
            return login;
        }
        return null;
    }

    @Override
    public Account getByCardNo(String cardNo) {
        return infoMapper.login(cardNo);
    }


    @Override
    public void transfer(String cardNo1, String cardNo2, double balance) {
        Account card1 = infoMapper.login(cardNo1);
        infoMapper.updateBalance(card1.getCardNo(),card1.getBalance()-balance);
        Account card2 = infoMapper.login(cardNo2);
        infoMapper.updateBalance(card2.getCardNo(),card2.getBalance()+balance);

        Record record1 = new Record();
        record1.setCardNo(card1.getCardNo());
        record1.setTransaction_date(new Date());
        record1.setExpense(balance);
        record1.setBalance(card1.getBalance()-balance);
        record1.setTransaction_type("消费");
        infoMapper.addRecord(record1);

        Record record2 = new Record();
        record2.setCardNo(card2.getCardNo());
        record2.setTransaction_date(new Date());
        record2.setIncome(balance);
        record2.setBalance(card2.getBalance()+balance);
        record2.setTransaction_type("存入");
        infoMapper.addRecord(record2);

    }

    @Override
    public List<Record> getAllRecord(String start, String end,String cardNo, int pageNo, int pageSize) {
        List<Record> allRecord = infoMapper.getAllRecord(start, end, cardNo,(pageNo - 1) * pageSize, pageSize);
        return allRecord;
    }

    @Override
    public int getCount(String start, String end,String cardNo) {
        return infoMapper.getCount(start,end,cardNo);
    }

    @Override
    public int updatePwd(String cardNo, String password) {
        return infoMapper.updatePwd(cardNo,password);
    }
}
